/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import conn.EMIS4Connection;
import dao.ClinicDao;
import dao.PersonDao;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import obj.Clinic;
import obj.Person;

/**
 *
 * @author nknowles
 */
public class AppointmentUtil extends DateUtils {

  /**
   * Fills in the Clinic combo box based off what's in the database.
   * @param clinicComboBox name of the clinic combo box
   */
  public static void setClinic(JComboBox clinicComboBox) {
    EMIS4Connection conn = EMIS4Connection.getInstance();
    List<Clinic> clinicList = null;
    ClinicDao clinicDAO = new ClinicDao();
    try {
      conn.connect();
      clinicList = clinicDAO.loadAll(conn.getConnection());
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
    for (Clinic clinicItem : clinicList) {
      clinicComboBox.addItem(clinicItem.getClinicName());
    }
  }

  /**
   * Sets the date and time combo boxes to reflect the current time rounded up to the nearest half hour.
   * @param yearComboBox name of year combo box
   * @param monthComboBox name of month combo box
   * @param dayComboBox name of day combo box
   * @param timeComboBox name of time combo box
   * @param ampmComboBox  name of AM/PM combo box
   */
  public static void setDateTime(JComboBox yearComboBox, JComboBox monthComboBox, JComboBox dayComboBox, JComboBox timeComboBox, JComboBox ampmComboBox, JLabel dayLabel) {
    String dt = currentDateTime();
    String time;
    int hour, min;
    int am = 0;

    yearComboBox.setSelectedItem(dt.substring(0, 4));
    monthComboBox.setSelectedIndex(Integer.valueOf(dt.substring(5, 7)) - 1);
    dayComboBox.setSelectedItem(dt.substring(8, 10));
    hour = Integer.valueOf(dt.substring(11, 13));
    if (hour >= 13) {
      am = 1;
      hour = hour % 12;
    } else if (hour == 0) {
      hour = 12;
    } else if (hour >= 12) {
      am = 1;
    }
    min = Integer.valueOf(dt.substring(14, 16));
    if (min > 30) {
      min = 0;
      if (++hour == 12) {
        am ^= 1;
      }
    } else {
      min = 3;
    }
    time = hour + ":" + min + "0";
    timeComboBox.setSelectedItem(time);
    ampmComboBox.setSelectedIndex(am);
    dayLabel.setText(currentDay());
  }

  public static List<Person> fillDoctors(JComboBox doctorComboBox) {
    EMIS4Connection conn = EMIS4Connection.getInstance();
    List<Person> doctorList = null;
    Person doctor = new Person();
    PersonDao personDAO = new PersonDao();

    doctor.setRoleID(4);
    try {
      conn.connect();
      doctorList = personDAO.searchMatching(conn.getConnection(), doctor);
    } catch (SQLException ex) {
      Logger.getLogger(AppointmentUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    for (Person doctorItem : doctorList) {
      doctorComboBox.addItem(doctorItem.getLastName() + ", " + doctorItem.getFirstName());
    }
    return doctorList;
  }

  public static String makeDateString(int year, int month, int day, int time, int ampm) {
    int currentYear = Calendar.getInstance().get(Calendar.YEAR);
    int hour, min;
    StringBuilder sb = new StringBuilder();
    sb.append(year + currentYear).append("-");
    sb.append(month + 1).append("-");
    sb.append(day + 1).append("-");
    hour = (time / 2) + (ampm * 12);
    sb.append(hour).append("-");
    min = (time % 2) * 30;
    if (min == 0) {
      sb.append(min).append("0");
    } else {
      sb.append(min);
    }
    return sb.toString();
  }
}
